package com.topofstack.flp.ui;

import java.io.File;
import java.io.IOException;

import com.bappi.custom.activities.CustomActivity;
import com.topofstack.flp.FLPApplication;
import com.topofstack.flp.R;
import com.topofstack.flp.utils.ImageAdapter;
import com.topofstack.flp.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;

public class GalleryActivity extends CustomActivity implements OnClickListener {
	
	Gallery gallery;
	ImageView imageview;
	FLPApplication application;
	File[] imageFiles;
	
	boolean is_delete;
	boolean is_export;
	boolean is_video;
	
	Button btnDelete;
	Button btnExport;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_gallery);
		
		if(getIntent().getExtras() != null){
			is_delete = getIntent().getExtras().getBoolean("is_delete", false);
			is_export = getIntent().getExtras().getBoolean("is_export", false);
			is_video = getIntent().getExtras().getBoolean("is_video", false);
		}
		else{
			is_delete = false;
			is_export = false;
			is_video = false;
		}
		
		btnDelete = (Button)findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(this);
		
		btnExport = (Button)findViewById(R.id.btnExport);
		btnExport.setOnClickListener(this);
		
		if(is_delete == false){
			btnDelete.setVisibility(View.GONE);
		}
		else{
			btnDelete.setVisibility(View.INVISIBLE);
		}
		
		if(is_export == false){
			btnExport.setVisibility(View.GONE);
		}
		else{
			btnExport.setVisibility(View.INVISIBLE);
		}
		
		application = (FLPApplication)getApplication();
		gallery = (Gallery)findViewById(R.id.gallery);
		imageview = (ImageView)findViewById(R.id.ImageView01);
		
		refreshGallery();
		
		gallery.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bitmap bmp = ImageAdapter.decodeSampledBitmapFromFile(imageFiles[arg2].getAbsolutePath(), 200, 200);
				imageview.setImageBitmap(bmp);
				
				if(is_delete){
					btnDelete.setVisibility(View.VISIBLE);
				}
				
				if(is_export){
					btnExport.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	
	
	private void refreshGallery(){
		imageFiles = application.getAssetManager().getAllImageFiles();
		ImageAdapter ia = new ImageAdapter(this, imageFiles);
		gallery.setAdapter(ia);
	}

	@Override
	public void onClick(View sender) {
		if(sender == btnDelete){
			int position = gallery.getSelectedItemPosition();
			if(position != -1){
				imageFiles[position].delete();
				Utils.showAlertMessage(getParent(), "", "Image deleted successfully");
				finish();
			}
		}
		else if(sender == btnExport){
			int position = gallery.getSelectedItemPosition();
			if(position != -1){
				String name = imageFiles[position].getName(); 
				String src_path = application.getAssetManager().imagePathFromName(name);
				String dst_path = application.getAssetManager().exportImagePathFromName(name);
				File dst_file = new File(dst_path);
				try {
					dst_file.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				application.getAssetManager().moveFile(src_path, dst_path,this,false);
//				sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ application.getAssetManager().exportImagePathFromName(""))));
				Utils.showAlertMessage(getParent(), "", "Image exported successfully");
			}
		}
	}
}
